package com.zjt.takeoutproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjt.takeoutproject.common.R;
import com.zjt.takeoutproject.entity.Employee;
import com.zjt.takeoutproject.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    //员工登录（request是为了登录后获取保存在本地储存里的员工信息）
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.MD5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.查数据库是否存在该用户
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee employee1 = employeeService.getOne(queryWrapper);
        if (employee1 == null) {//为空
            return R.error("用户不存在");
        }

        //3.核对密码
        if (!employee1.getPassword().equals(password)){
            return R.error("密码不正确");
        }

        //4.查看状态是否可用
        if (employee1.getStatus()==0){
            return R.error("账号已禁用");
        }

        //5.将员工id存入Session中，返回成功结果
        request.getSession().setAttribute("employee",employee1.getId());
        return R.success(employee1);
    }

    //退出
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //1.清理Session用户id
        request.getSession().removeAttribute("employee");
        //2.返回结果
        return R.success("退出登录成功");
    }

    //新增员工
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工，员工信息：{}",employee.toString());

        //设置初始密码并加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        公共字段更新完成
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long empId= (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    //员工信息的分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页的构造器
        Page page1 = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        employeeService.page(page1,queryWrapper);
        return R.success(page1);
    }

    //根据ID修改员工信息
    @PutMapping
    public R<String> update(HttpServletRequest httpServletRequest,@RequestBody Employee employee){
//        Long empId= (Long) httpServletRequest.getSession().getAttribute("employee");
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);

        return R.success("修改员工信息成功");
    }

    //根据ID查询员工信息
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee id1 = employeeService.getById(id);
        if (id1 != null) {
            return R.success(id1);
        }
        return R.error("没有查询到该员工信息");
    }
}
